package com.chuzhong.sf.gui;

import com.chuzhong.sf.api.login_api;
import jdk.nashorn.internal.scripts.JD;

import javax.print.attribute.standard.JobMessageFromOperator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @Author 初衷.
 * @Date 2020/10/16
 **/
public class update_password extends JFrame {


    private static int count=0;
    private static JButton bt1;//确定按钮
    private static JLabel jl_1;//登录的版面
    private static JFrame jf_1;//登陆的框架
    private static JPasswordField jtext1;//用户名
    private static JPasswordField jtext2;//密码
    private static JLabel jl_admin;
    private static JLabel jl_password;
    public update_password(String username,String password){
        Font font =new Font("宋体", Font.PLAIN, 20);//设置字体
        jf_1=new JFrame("修改密码页面");
        jf_1.setSize(500, 400);

        jl_1=new JLabel();

        jl_admin=new JLabel("原密码:");
        jl_admin.setBounds(20, 50, 100, 50);
        jl_admin.setFont(font);

        jl_password=new JLabel("新密码:");
        jl_password.setBounds(20, 120, 100, 50);
        jl_password.setFont(font);


        bt1=new JButton("确定");         //更改成loginButton
        bt1.setBounds(90, 250, 100, 50);
        bt1.setFont(font);


        //加入文本框
        jtext1=new JPasswordField();
        jtext1.setBounds(150, 50, 250, 50);
        jtext1.setFont(font);

        jtext2=new JPasswordField();//密码输入框
        jtext2.setBounds(150, 120, 250, 50);
        jtext2.setFont(font);
        jl_1.add(jtext1);
        jl_1.add(jtext2);
        jl_1.add(jl_admin);
        jl_1.add(jl_password);
        jl_1.add(bt1);

        jf_1.add(jl_1);
        jf_1.setVisible(true);
        jf_1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//        jf_1.setLocation(300,400);
        jf_1.setLocationRelativeTo(null);

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                char[] a = jtext1.getPassword();//old_password
                char[] b = jtext2.getPassword();//new_password
                String old_password = String.valueOf(a);
                String new_password = String.valueOf(b);
                if(old_password.equals(password)){//如果两个密码一样就成功
                    if (new_password.length()!=0){
                        try {
                            String user_password = login_api.update_user_password(username, new_password);
                            System.out.println(user_password);
                            if(user_password.equals("1")){
                                JOptionPane.showMessageDialog(null,"修改密码成功！！");
                                jf_1.dispose();
                            }
                            else {
                                JOptionPane.showMessageDialog(null,"修改密码失败！请联系管理员","异常通知",JOptionPane.ERROR_MESSAGE);
                            }
                        } catch (IOException ex) {

                            ex.printStackTrace();
                        }
                    }
                }else{
                    JOptionPane.showMessageDialog(null,"原密码输入错误，请重新输入","警告",JOptionPane.WARNING_MESSAGE);
                }

            }
        };

        bt1.addActionListener(actionListener);
    }

    public static void main(String[] args) {

    }
}
